home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / NervousText.java < prev    next >
Text File  |  1998-09-15  |  4KB  |  138 lines

  1. /*
  2.  * @(#)NervousText.java    1.3 98/03/23
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import java.awt.event.*;
  32. import java.awt.Graphics;
  33. import java.awt.Font;
  34. import java.applet.Applet;
  35.  
  36. /**
  37.  * An applet that displays jittering text on the screen.
  38.  *
  39.  * @author Daniel Wyszynski 04/12/95
  40.  * @version 1.10, 02/05/97
  41.  * @modified 05/09/95 kwalrath Changed string; added thread suspension
  42.  * @modified 02/06/98 madbot removed use of suspend and resume and cleaned up
  43.  */
  44.  
  45. public class NervousText extends Applet implements Runnable, MouseListener {
  46.     String banner;        // The text to be displayed
  47.     char bannerChars[];        // The same text as an array of characters
  48.     Thread runner = null;    // The thread that is displaying the text
  49.     boolean threadSuspended;    // True when thread suspended (via mouse click)
  50.  
  51.     public void init() {
  52.     banner = getParameter("text");
  53.     if (banner == null) {
  54.         banner = "HotJava";
  55.     }
  56.         int bannerLength = banner.length();
  57.     bannerChars =  new char[bannerLength];
  58.         banner.getChars(0, banner.length(), bannerChars, 0);
  59.  
  60.         threadSuspended = false;
  61.  
  62.     resize(15*(bannerLength + 1), 50);
  63.     setFont(new Font("TimesRoman", Font.BOLD, 36));
  64.     addMouseListener(this);
  65.     }
  66.  
  67.     public void destroy() {
  68.         removeMouseListener(this);
  69.     }
  70.  
  71.     public void start() {
  72.         runner = new Thread(this);
  73.         runner.start();
  74.     }
  75.  
  76.     public synchronized void stop() {
  77.     runner = null;
  78.         if (threadSuspended) {
  79.             threadSuspended = false;
  80.             notify();
  81.         }
  82.     }
  83.  
  84.     public void run() {
  85.         Thread me = Thread.currentThread();
  86.         while (runner == me) {
  87.             try {
  88.                 Thread.sleep(100);
  89.                 synchronized(this) {
  90.                     while (threadSuspended) {
  91.                         wait();
  92.                     }
  93.                 }
  94.             } catch (InterruptedException e){
  95.             }
  96.             repaint();
  97.         }
  98.     }
  99.  
  100.     public void paint(Graphics g) {
  101.         for(int i=0, length = banner.length(); i<length; i++) {
  102.             int x = (int) (10*Math.random() + 15*i);
  103.             int y = (int) (10*Math.random() + 36);
  104.             g.drawChars(bannerChars, i, 1, x, y);
  105.     }
  106.     }
  107.  
  108.     public synchronized void mousePressed(MouseEvent e) {
  109.         e.consume();
  110.         threadSuspended = !threadSuspended;
  111.         if (!threadSuspended)
  112.             notify();
  113.     }
  114.  
  115.     public void mouseReleased(MouseEvent e) {
  116.     }
  117.  
  118.     public void mouseEntered(MouseEvent e) {
  119.     }
  120.  
  121.     public void mouseExited(MouseEvent e) {
  122.     }
  123.  
  124.     public void mouseClicked(MouseEvent e) {
  125.     }
  126.  
  127.     public String getAppletInfo() {
  128.         return "Title: NervousText\nAuthor: Daniel Wyszynski\nDisplays a text banner that jitters.";
  129.     }  
  130.   
  131.     public String[][] getParameterInfo() {
  132.         String pinfo[][] = {
  133.             {"text", "string", "Text to display"},
  134.         };
  135.         return pinfo;
  136.     }
  137. }
  138.